package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import com.example.dao.CardluliInfoDao;
import com.example.entity.*;
import com.example.entity.CardluliInfo;
import com.example.vo.CardluliInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class CardluliInfoService {

    @Resource
    private CardluliInfoDao cardluliInfoDao;
	
	
	//kuabiaojisuan3

    public CardluliInfo add(CardluliInfo cardluliInfo) {
        List<Long> picflst = cardluliInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            cardluliInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
        // 唯一校验
        
      // cardluliInfo.setAccount((double) 0);
	  
        cardluliInfoDao.insertSelective(cardluliInfo);
        return cardluliInfo;
    }
	
	public List<Map<String,String>> findByDiqu() {
        return cardluliInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        cardluliInfoDao.deleteByPrimaryKey(id);
    }

    public void update(CardluliInfo cardluliInfo) {
        List<Long> picflst = cardluliInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            cardluliInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
		//youdianzan
        cardluliInfoDao.updateByPrimaryKeySelective(cardluliInfo);
    }

    public CardluliInfo findById(Long id) {
        return cardluliInfoDao.selectByPrimaryKey(id);
    }
	
    public List<CardluliInfoVo> findAll() {
        return cardluliInfoDao.findByFoodtypes("all");
    }

    public PageInfo<CardluliInfoVo> findPage(String foodtypes, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<CardluliInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = cardluliInfoDao.findByFoodtypes(foodtypes);}
		
        
        return PageInfo.of(all);
    }
	
	public PageInfo<CardluliInfoVo> findPageqt(String foodtypes, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<CardluliInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = cardluliInfoDao.findByFoodtypes(foodtypes);
		
	
	
        
        return PageInfo.of(all);
    }

   // public CardluliInfoVo findByUserName(String name) {
//        return cardluliInfoDao.findByUsername(name);
//    }

	//yoxulogindenxglu
	
    public void changeStatus(Long id) {
        CardluliInfo cardluliInfo = cardluliInfoDao.selectByPrimaryKey(id);
        if(cardluliInfo.getStatus().equals("是")){
            cardluliInfo.setStatus("否");
            cardluliInfoDao.updateByPrimaryKey(cardluliInfo);
        }else if(cardluliInfo.getStatus().equals("否")){
            cardluliInfo.setStatus("是");
            cardluliInfoDao.updateByPrimaryKey(cardluliInfo);
        }
    }
	//ddaizdhifu
	//youtixing
	
	
	
	
	
	//xiugaimimazys
	
}
